package com.bbjob.service.impl;

import java.io.File;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.bbjob.client.result.ResultForExaminee;
import com.bbjob.dto.Result;
import com.bbjob.model.SysOrganizationStu;
import com.bbjob.query.QueryExaminee;
import com.bbjob.service.IExamineeService;
import com.bbjob.toolutil.HttpClientUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

@Service
public class ExamineeServiceImpl implements IExamineeService{

	@Value("${sys.uniquecode}")
	private String uniquecode;
	@Value("${examol.center.url}")
	private String centerUrl;
	@Value("${examol.center.entry}")
	private String centerEntry;
	@Value("${examol.center.import}")
	private String centerImport;	
	@Value("${examol.center.stuList}")
	private String centerStuList;
	@Value("${examol.center.stu}")
	private String centerStu;
	@Value("${examol.center.stuEdit}")
	private String centerStuEdit;
	@Value("${examol.center.stuDel}")
	private String centerStuDel;
	@Value("${sys.local.file.path}")
	private String localFileDir;

	
	@Override
	public Result entryInfo(SysOrganizationStu stu) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("uniquecode", uniquecode);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			if (stu.getSno() != null && !"".equals(stu.getSno())) {
				params.put("sno", stu.getSno());
			}

			if (stu.getSname() != null && !"".equals(stu.getSname())) {
				params.put("sname", stu.getSname());
			}
			
			if (stu.getSex() != null) {
				params.put("sex", stu.getSex());
			}
			
			if (stu.getOrganSchool() != null) {
				params.put("organSchool", stu.getOrganSchool());
			}
			
			if (stu.getOrganDepart() != null) {
				params.put("organDepart", stu.getOrganDepart());
			}
			
			if (stu.getOrganGrade() != null) {
				params.put("organGrade", stu.getOrganGrade());
			}
			
			if (stu.getOrganClass() != null) {
				params.put("organClass", stu.getOrganClass());
			}
			
			if (stu.getBirthday() != null) {
				params.put("birthdayStr", sdf.format(stu.getBirthday()));
			}
			if (stu.getIdentityNumber() != null && !"".equals(stu.getIdentityNumber())) {
				params.put("identityNumber", stu.getIdentityNumber());
			}
			if (stu.getStatus() != null) {
				params.put("status", stu.getStatus());
			}
			if (stu.getRemark() != null && !"".equals(stu.getRemark())) {
				params.put("remark", stu.getRemark());
			}
			
			String resultStr = HttpClientUtil.doPost(centerUrl + centerEntry, params);
			
		    GsonBuilder builder = new GsonBuilder();
		    
		    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
			
			Result result = builder.create().fromJson(resultStr, Result.class);
			
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}

	@Override
	public Map<String, Object> importExaminee(MultipartFile multipartFile, HttpServletRequest request) {
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		
		if (multipartFile == null || multipartFile.isEmpty()) {
			
			returnMap.put("code", 500);
			returnMap.put("message", "文件为空");
			returnMap.put("data", null);
			return returnMap;
		}

		String saveDirectory = localFileDir + uniquecode +"/" + sdf.format(new Date()) + "/" + multipartFile.getOriginalFilename();
		
		try {
			File path = new File(saveDirectory);
			
			if (!path.getParentFile().exists()) {
				path.getParentFile().mkdirs();
				path.createNewFile();
			}
			
			multipartFile.transferTo(path);
			
			Map<String, Object> params = new HashMap<String, Object>();
			
			params.put("uniquecode", uniquecode);
			params.put("filename", multipartFile.getOriginalFilename());
			
			String result = HttpClientUtil.doPost(centerUrl + centerImport, path, params);
			
			returnMap = new Gson().fromJson(result, HashMap.class);
			
			return returnMap;
			
		} catch(Exception e) {
			e.printStackTrace();
			returnMap.put("code", 500);
			returnMap.put("message", "文件上传失败");
			returnMap.put("data", null);
			
			return returnMap;
		}
	}

	@Override
	public Result selectByParams(QueryExaminee examinee) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		
		try {
			params.put("uniquecode", uniquecode);
			params.put("pageNum", examinee.getPageNum());
			params.put("pageSize", examinee.getPageSize());
			
			if (examinee.getSno() != null && !"".equals(examinee.getSno())) {
				params.put("sno", examinee.getSno());
			}

			if (examinee.getSname() != null && !"".equals(examinee.getSname())) {
				params.put("sname", examinee.getSname());
			}
			
			if (examinee.getSex() != null) {
				params.put("sex", examinee.getSex());
			}
			
			if (examinee.getIdentityNumber() != null && !"".equals(examinee.getIdentityNumber())) {
				params.put("identityNumber", examinee.getIdentityNumber());
			}
			
			if (examinee.getOrganSchool() != null) {
				params.put("organSchool", examinee.getOrganSchool());
			}
			
			if (examinee.getOrganDepart() != null) {
				params.put("organDepart", examinee.getOrganDepart());
			}
			
			if (examinee.getOrganGrade() != null) {
				params.put("organGrade", examinee.getOrganGrade());
			}
			
			if (examinee.getOrganClass() != null) {
				params.put("organClass", examinee.getOrganClass());
			}
			
			String resultStr = HttpClientUtil.doPost(centerUrl + centerStuList, params);
			
			Result list = new Gson().fromJson(resultStr, Result.class);
			
			return list;
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public SysOrganizationStu selectByPrimaryKey(Integer id) {
		
		try {
			
			String resultStr = HttpClientUtil.doGet(centerUrl + centerStu + id);
			
		    GsonBuilder builder = new GsonBuilder();
		    
		    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { 
		    	@Override   
		    	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
		    		return new Date(json.getAsJsonPrimitive().getAsLong()); 
		    	}
		    });
		    
			ResultForExaminee result = builder.create().fromJson(resultStr, ResultForExaminee.class);
		
			if (result.getCode() == 200) {
				
				return result.getData();
			} else {
				
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public Result updateStudent(SysOrganizationStu examinee, Integer id) {
		
		Map<String, Object> params = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		params.put("uniquecode", uniquecode);
		try {
			if (examinee.getSno() != null && !"".equals(examinee.getSno())) {
				params.put("sno", examinee.getSno());
			}
			if (examinee.getSname() != null && !"".equals(examinee.getSname())) {
				params.put("sname", examinee.getSname());
			}
			if (examinee.getSex() != null) {
				params.put("sex", examinee.getSex());
			}
			if (examinee.getOrganDepart() != null) {
				params.put("organDepart", examinee.getOrganDepart());
			}
			if (examinee.getOrganGrade() != null) {
				params.put("organGrade", examinee.getOrganGrade());
			}
			if (examinee.getOrganClass() != null) {
				params.put("organClass", examinee.getOrganClass());
			}
			if (examinee.getBirthday() != null) {
				params.put("birthdayStr", sdf.format(examinee.getBirthday()));
			}
			if (examinee.getIdentityNumber() != null && !"".equals(examinee.getIdentityNumber())) {
				params.put("identityNumber", examinee.getIdentityNumber());
			}
			if (examinee.getStatus() != null) {
				params.put("status", examinee.getStatus());
			}
			if (examinee.getRemark() != null && !"".equals(examinee.getRemark())) {
				params.put("remark", examinee.getRemark());
			}
			
			String resultStr = HttpClientUtil.doPost(centerUrl + centerStuEdit + id, params);
			
			Result result = new Gson().fromJson(resultStr, Result.class);
			
			return result;
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public void delById(Integer id) {
		try {
			String resultStr = HttpClientUtil.doGet(centerUrl + centerStuDel + id);
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}
